<?php

class ProductController extends Controller {
    
    public function actionIndex() {
        $this->render("index");
    }
    
        
    //Config Table Product Category
    function actionProductCategory() {
        $model = new ProductCategory();
        $this->render("ProductCategory", array(
            "model" => $model
        ));
    }
    function actionProductCategoryForm($id=NULL) {
        $model = new ProductCategory();
        
        // Save
        if (!empty($_POST["ProductCategory"])) {
            if (!empty($_POST["ProductCategory"]["id"])) {
                $id = $_POST["ProductCategory"]["id"];
                $model = ProductCategory::model()->findByPk($id);
            }
            $model->_attributes = $_POST["ProductCategory"];
            
            if ($model->save()) {
                $this->redirect(array("Product/ProductCategory"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $model = ProductCategory::model()->findByPk($id);
        }
        
        $this->render("ProductCategoryForm", array(
            "model" => $model
        ));
    }
    
    function actionProductCategoryDelete($id) {
        ProductCategory::model()->deleteByPk($id);
        $this->redirect("index.php?r=Product/ProductCategory");
    }
    //End Config Table Product Category
    
    //Config Table ts_product
    function actionProduct() {
        $model = new Product();
        $this->render("Product", array(
            "model" => $model
        ));
    }
    function actionProductForm($id=NULL) {
        $model = new Product();
        
        // Save
        if (!empty($_POST["Product"])) {
            if (!empty($_POST["Product"]["id"])) {
                $id = $_POST["Product"]["id"];
                $model = Product::model()->findByPk($id);
                $currentName = $model->product_picture_name;
            }
            
            $rnd = rand(0,9999);///edit
            
            $model->_attributes = $_POST["Product"];
            
            
            $uploadedFile=CUploadedFile::getInstance($model,'product_picture_name');
            $fileName = "{$rnd}-{$uploadedFile}";  // random number + file name
//            $fileName = "{$rnd}.jpg";
            
            if($uploadedFile != NULL)
            {
                $model->product_picture_name = $fileName;    
            }
            else
            {
                $model->product_picture_name = $currentName;
            }
            
            if ($model->save()) {
                if($uploadedFile != NULL)
                {
                $uploadedFile->saveAs("images/product/".$fileName); 
//                $uploadedFile->saveAs(Yii::app()->basePath.'/../images/product/'.$fileName); 
                }
                $this->redirect(array("Product/Product"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $model = Product::model()->findByPk($id);
        }
        
        $this->render("ProductForm", array(
            "model" => $model
        ));
    }
    
    function actionProductDelete($id) {
        Product::model()->deleteByPk($id);
        $this->redirect("index.php?r=Product/Product");
    }
    //End Config Table mt_company_promotion
    
    
    //Config Table Stock
    function actionStockProduct() {
        $model = new Stock();
        $this->render("StockProduct", array(
            "model" => $model
        ));
    }
    function actionStockProductForm($id=NULL) {
        $model = new Stock();
        
        // Save
        if (!empty($_POST["Stock"])) {
            if (!empty($_POST["Stock"]["id"])) {
                $id = $_POST["Stock"]["id"];
                $model = Stock::model()->findByPk($id);
            }
            $model->_attributes = $_POST["Stock"];
            
            if ($model->save()) {
                $this->redirect(array("Product/StockProduct"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $model = Stock::model()->findByPk($id);
        }
        
        $this->render("StockProductForm", array(
            "model" => $model
        ));
    }
    
    function actionStockProductDelete($id) {
        Stock::model()->deleteByPk($id);
        $this->redirect("index.php?r=Product/StockProduct");
    }
    //End Config Table Stock
    
    
    
    //Config Table mt_company_news
    function actionCompanyNews() {
        $model = new CompanyNews();
        $this->render("CompanyNews", array(
            "model" => $model
        ));
    }
    function actionCompanyNewsForm($id=NULL) {
        $model = new CompanyNews();
        
        // Save
        if (!empty($_POST["CompanyNews"])) {
            if (!empty($_POST["CompanyNews"]["id"])) {
                $id = $_POST["CompanyNews"]["id"];
                $model = CompanyNews::model()->findByPk($id);
            }
            $model->_attributes = $_POST["CompanyNews"];
            
            if ($model->save()) {
                $this->redirect(array("Company/CompanyNews"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $model = CompanyNews::model()->findByPk($id);
        }
        
        $this->render("CompanyNewsForm", array(
            "model" => $model
        ));
    }
    
    function actionCompanyNewsDelete($id) {
        CompanyNews::model()->deleteByPk($id);
        $this->redirect("index.php?r=Company/CompanyNews");
    }
}

?>
